package com.daydayup.itext.word2pdf.documents4j;

import com.documents4j.api.DocumentType;
import com.documents4j.api.IConverter;
import com.documents4j.job.LocalConverter;

import java.io.*;

/**
 * @author luo_zm
 * @DESCRIPTION
 * @create 2020/4/11 14:53
 */
public class Document4jApp {
    public static void main(String[] args) {
        String docx = "docx";
        String doc = "doc";
        String wordDocx = "C:\\Users\\Administrator\\Desktop\\1339257692094496.docx";
        String wordDoc = "C:\\Users\\Administrator\\Desktop\\test.doc";
        String pdf = "C:\\Users\\Administrator\\Desktop\\Test_out_" + System.currentTimeMillis() / 1000 + ".pdf";
        File inputWord = new File(wordDocx);
        File outputFile = new File(pdf);
        try {
            InputStream docxInputStream = new FileInputStream(inputWord);
            OutputStream outputStream = new FileOutputStream(outputFile);
            IConverter converter = LocalConverter.builder().build();
            String inFileName = inputWord.getName();
            String substring = inFileName.substring(inFileName.lastIndexOf(".") + 1);
            if (docx.equalsIgnoreCase(substring)) {
                converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
            } else if ((doc.equalsIgnoreCase(substring))) {
                converter.convert(docxInputStream).as(DocumentType.DOC).to(outputStream).as(DocumentType.PDF).execute();

            }
            //一定要记得关闭流
            converter.shutDown();
            docxInputStream.close();
            outputStream.close();

            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
